package com.qfjy.controller;

import com.qfjy.common.utils.ExcelUtils;
import com.qfjy.common.utils.common.utils.redis.RedisRepeatUtils;
import com.qfjy.entity.po.User;
import com.qfjy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author guoweixin
 * @Date 2022/9/30
 * @Version 1.0
 */
@Controller
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /** 重复提交解决方案Redis Token*/
    @Autowired
    private RedisRepeatUtils redisRepeatUtils;

    /**
     * TODO Redis 集成
     */
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * TODO 进入用户管理页面
     */
    @GetMapping("list")
    public String userListToPage(HttpServletRequest request){

        List<User> list=userService.selectList();

        request.setAttribute("list",list);
        return "user/list";
    }


    /**
     * TODO 进入Excel导入页面
     */
    @GetMapping("importToPage")
    public String importToPage(HttpServletRequest request){
        /**生成token repeated generate*/
        redisRepeatUtils.repeatedGenerateToken(request,"user:importExcel:");

        return "user/importExcel";
    }


    /**
     * TODO Excel数据导入
     *    接收前端Excel文件进行数据导入
     */
    @PostMapping("importExcel")
    @ResponseBody
    public int importExcel(@RequestParam("file123")MultipartFile file,HttpServletRequest request){
        log.info("进来了："+System.currentTimeMillis());

        if(false==redisRepeatUtils.repeatedCheck(request,"user:importExcel:")){
             return -1;
        }


        int num=0;
        try {
            //Excel导入功能
            List<User> list= ExcelUtils.importExcel(file,User.class);


            //去重功能
           // list= this.repeatEmail(list);
            Thread.sleep(2000);
            //批量添加功能
           num= userService.insertBatch(list);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return num;
    }


    /**
     * TODO 去重功能（email)
     *  1、查询MYSQL所有的邮件数据
     *  2、Excel导入列表数据
     */
    public List<User> repeatEmail( List<User> listExcel){
        //数据库查询的 Email
       List<String> listDB=userService.selectEmails();

       for(int i=0;i<listExcel.size();i++){

           User u=listExcel.get(i);

           if(listDB.contains(u.getEmail())){
                listExcel.remove(u);
                i--;
           }

       }
        return listExcel;
    }


    /**
     * TODO 根据ID删除信息
     */
    @ResponseBody
    @PostMapping("deleteById")
    public int deleteUserById(@RequestParam("id") final int id){

        return userService.deleteByPrimaryKey(id);
    }
    /**
     * TODO 批量删除
     * ids: 1,2,3,4,5
     */
    @PostMapping("deleteBatch")
    @ResponseBody
    public int deleteBatchByIds(@RequestParam("idsStr") String idsStr){
        String[] ids=idsStr.split(",");
        int num=userService.deleteBatch(ids);
        log.info("批量删除{}",num);
        return num;
    }


    /**
     * TODO 状态修改
     * @param status  状态 0无效 1有效
     * @param id  主键唯一标识
     */
    @PostMapping("updateStatusById")
    @ResponseBody
    public int updateStatusById(@RequestParam("status") final int status,
                                @RequestParam("id") final int id){

        return userService.updateStatusById(status,id);
    }

}
